﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBase;
using System.Data;
using MvcBase;

namespace NapCardOnline.Models
{

    public partial class User
    {
        #region Methods
        /// <summary>
        /// Updates the balance.
        /// </summary>
        /// <param name="objTransferFund">The obj transfer fund.</param>
        /// <returns>
        /// True if update successfull else false
        /// </returns>
        public static bool UpdateBalanceWhenTransferFund(TransferFund objTransferFund)
        {
            bool isUpdate = false;
            try
            {

                //FromUser
                var objFromUser = GetUserById(objTransferFund.FK_FromUserID);
                //So du TK nguoi chuyen =So du TK nguoi chuyen - so tien chuyen + Chiet khau
                objFromUser.UserBalance = objFromUser.UserBalance - objTransferFund.TransferFundAmount +
                                            objTransferFund.TransferFundCommissionAmount;
                objFromUser.AAUpdateDate = DateTime.Now;

                //ToUser
                var objToUser = GetUserById(objTransferFund.FK_ToUserID);
                objToUser.UserBalance = objToUser.UserBalance + objTransferFund.TransferFundAmount;
                objToUser.AAUpdateDate = DateTime.Now;

                using (NapCardOnlineEntities db = new NapCardOnlineEntities())
                {
                    using (var transaction = MvcBase.BaseTransaction.BeginTransaction(db))
                    {

                        //Save user from
                        db.Users.Attach(objFromUser);
                        db.ObjectStateManager.ChangeObjectState(objFromUser, EntityState.Modified);
                        db.SaveChanges();

                        //Save user to
                        db.Users.Attach(objToUser);
                        db.ObjectStateManager.ChangeObjectState(objToUser, EntityState.Modified);
                        db.SaveChanges();
                        isUpdate = true;
                        transaction.Commit();
                    }
                }
            }
            catch (Exception)
            {
                isUpdate = false;
            }

            return isUpdate;
        }

        /// <summary>
        /// Gets the user by id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns>The user object</returns>
        public static User GetUserById(int? userId)
        {
            using (NapCardOnlineEntities db = new NapCardOnlineEntities())
            {
                var objUser = db.Users.FirstOrDefault(u => u.UserID == userId);
                return objUser;
            }
        }

        /// <summary>
        /// Gets the balance by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns>The balance of User</returns>
        public static double? GetBalanceByUserId(int userId)
        {
            double? balance = 0;
            var objUser = GetUserById(userId);
            if (objUser != null)
            {
                balance = objUser.UserBalance;
            }
            return balance;
        }

        /// <summary>
        /// Gets the commision percent
        /// </summary>
        /// <param name="fromUserID">From user ID.</param>
        /// <param name="toUserID">To user ID.</param>
        /// <returns>
        /// Commision percent
        /// </returns>
        public static double? GetCommisionPercent(int fromUserID, int toUserID)
        {
            double? percent = 0;
            double? commissionFromUser = Models.CommissionLevel.GetTransferFundCommissionByUserID(fromUserID);
            double? commissionToUser = Models.CommissionLevel.GetTransferFundCommissionByUserID(toUserID);
            percent = commissionFromUser - commissionToUser;
            return percent;
        }
      
        /// <summary>
        /// Checks the user level.
        /// </summary>
        /// <param name="levelFrom">The level from.</param>
        /// <param name="levelTo">The level to.</param>
        /// <returns></returns>
        public static bool CheckUserLevel(int? levelFrom, int? levelTo)
        {
            bool ischeck = false;
            ischeck = levelFrom <= levelTo;
            return ischeck;
        }

        /// <summary>
        /// Checks the user blance.
        /// </summary>
        /// <param name="userFromBlance">The user from blance.</param>
        /// <param name="transferBlance">The transfer blance.</param>
        /// <returns>True if user from balance greater transfer balance else false</returns>
        public static bool CheckUserBlance(double? userFromBlance, double transferBlance)
        {
            bool ischeck = false;
            ischeck = userFromBlance >= transferBlance;
            return ischeck;
        }
        #endregion
    }
}